package com.zlh;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @package com.zlh
 * @company: dacheng
 * @author: zlh
 * @createDate: 2021/4/26
 */
public class JacksonDemo {
    public static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        String jsonStr = "{\"voName\":\"一个容器而已\",\"pers\":[{\"name\":\"张三\",\"age\":46,\"childs\":[{\"name\":\"小张三1\",\"age\":20,\"childs\":null},{\"name\":\"小张三2\",\"age\":17,\"childs\":null}]},{\"name\":\"李四\",\"age\":29,\"childs\":[{\"name\":\"小李四1\",\"age\":20,\"childs\":null}]},{\"name\":\"王二麻子\",\"age\":23,\"childs\":null}]}";
        // string 转 json
        JsonNode jsonNode = objectMapper.readTree(jsonStr);
        System.out.println("-----voName-------"+jsonNode.get("voName").toString());
        JsonNode perJsons = jsonNode.get("pers");
        System.out.println("perJsons是不是个集合：" + perJsons.isArray());
        // json和jsonArray都是JsonNode类型
        for (int i = 0; i < perJsons.size(); i++){
            JsonNode userNode = perJsons.get(i);
            System.out.println("-----name------" + userNode.get("name").toString());
            JsonNode childNodes = userNode.get("childs");
            if (childNodes !=null && childNodes.isArray()){
                for (int j = 0; j < childNodes.size(); j++){
                    JsonNode childNode = childNodes.get(j);
                    // 需要转换的类型get后进行转换
                    System.out.println("....cname..." + childNode.get("name").toString()
                                        +"...cage.."+childNode.get("age").asInt());
                }
            }
        }

        //json转集合
        List strList = objectMapper.readValue(perJsons.toString(), new TypeReference<List>() {});
        for (Object str:strList){
            System.out.println("json转集合: "+String.valueOf(str));
        }
        /**
         * json转复杂对象
         * 如果是 ArrayList<bean>那么使用ObjectMapper 的getTypeFactory().constructParametricType(ArrayList.class, bean.class);
         * 如果是 HashMap<String,bean>那么 ObjectMapper 的getTypeFactory().constructParametricType(HashMap.class,String.class, bean.class);
         */
        JavaType javaType = getCollectionType(ArrayList.class,Person.class);
        List<Person> personList = objectMapper.readValue(perJsons.toString(), javaType);
        for (Person person:personList){
            System.out.println("json转复杂对象："+person.toString());
        }
        //对象转json
        String personJson = objectMapper.writeValueAsString(personList);
        System.out.println("对象转json："+personJson);
    }

    /**
     * 获取泛型的Collection Type
     * @param collectionClass 泛型的Collection
     * @param elementClasses 元素类
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }
}
